% General purpose function for reading GalvoScanner log files
% Input is path to log file. Output is a matrix with data in the format:
%
function mLog = gs_readlogfile(sPath)
hFile = fopen(sPath, 'r');
% Get header
cHeader = {};
while 1
    tLine = fgetl(hFile);
    if strcmpi(tLine, 'header_end') || ~ischar(tLine), break, end
    cHeader{end+1} = tLine;
end
mLog = [];
while 1
    tLine = fgetl(hFile);
    if ~ischar(tLine), break, end
    mLog(end+1,:) = str2num(strrep(tLine, ',', ' '));
end
fclose(hFile);

% Plot (optional)
if 0
    sDate = cHeader{3};
    cTitles = regexp(cHeader{4},',','split');
    vTime = cumsum(mLog(:,1));
    hFig = figure; hold on
    mCols = get(hFig, 'defaultaxescolororder');
    for i = 2:size(mLog, 2)
        stairs(vTime/1000, mLog(:,i), 'o-', 'color', mCols(i,:));
    end
    xlabel('Time (sec)')
    hLeg = legend(cTitles);
    set(hLeg, 'interpreter', 'none')
end


return